package com.xiaobaibai.config;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableCaching
public class RedisCacheConfig {

//    @Bean
//    public RedisCacheConfiguration setRedisCacheConfiguration(){
//
//        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//
//        //修改value的序列化方式
//        //记得前方要重新赋值,因为方法返回的是一个新的config
//        config=config.serializeValuesWith
//                (RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//
//        config=config.entryTtl(Duration.ofHours(10));//设置过期时间
//
//        //设置缓存的前缀
//        config=config.prefixKeysWith("cache:");
//
//        return config;
//    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheManager cacheManager = new RedisCacheManager(
                RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
                //未指定key则12小时
                this.getRedisCacheConfigurationWithTtl(60 * 60 * 12), // 12小时
                //需要什么就自己添加方法
                this.getRedisCacheConfigurationMap() // 指定 key 策略
        );
        return cacheManager;
    }

    private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        //对优惠券特殊对待,缓存时间为30min
        redisCacheConfigurationMap.put("coupon", this.getRedisCacheConfigurationWithTtl(60*30));
        //对商品评价缓存特殊对待,缓存时间为3h,及时消除即可
//        redisCacheConfigurationMap.put("evaluation",this.getRedisCacheConfigurationWithTtl(60*60*3));
        //对单个商品缓存特殊对待,缓存时间为30min
        redisCacheConfigurationMap.put("oneProduct",this.getRedisCacheConfigurationWithTtl(60*30));
        //对用户地址缓存特殊对待,缓存时间为7day,及时消除即可
//        redisCacheConfigurationMap.put("address",this.getRedisCacheConfigurationWithTtl(60*60*24*7));
        return redisCacheConfigurationMap;
    }

    private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                RedisSerializationContext
                        .SerializationPair
                        .fromSerializer(new GenericJackson2JsonRedisSerializer())
        ).entryTtl(Duration.ofSeconds(seconds)).prefixKeysWith("cache:");

        return redisCacheConfiguration;
    }

}
